import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class SelfDio extends StatefulWidget {
  @override
  _SelfDioState createState() => _SelfDioState();
}

class _SelfDioState extends State<SelfDio> {
  List _list = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Dio 请求数据")),
      body: ListView.builder(
        itemCount: _list.length,
        itemBuilder: (BuildContext context, int i) => Text('${_list[i]["nm"]}'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          dioRequest2();
        },
        child: Icon(Icons.search),
      ),
    );
  }

  void dioRequest1() async {
    Dio dio = new Dio();
    Response response =
        await dio.get('http://m.maoyan.com/ajax/movieOnInfoList?token=');
    // print('movieList结果是${response.data["movieList"]}');

    Map<String, dynamic> responseData = json.decode(response.toString());
    print('movieList结果是${responseData["movieList"]}');

    setState(() {
      _list = responseData["movieList"];
    });
  }

  void dioRequest2() {
    try {
      Dio dio = new Dio();
      dio
          .get('http://m.maoyan.com/ajax/movieOnInfoList?token=')
          .then((Response response) {
        Map<String, dynamic> responseData = json.decode(response.toString());
        print('结果~数组~~~~${responseData["movieList"]}');
        setState(() {
          _list = responseData["movieList"];
        });
      });
    } catch (error) {
      print(error);
    }
  }
}
